package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.DWaybillService;
import cn.huiyunche.base.service.interfaces.DriverBillDetailsService;
import cn.huiyunche.base.service.mappers.DDriverBillDetailsMapper;
import cn.huiyunche.base.service.model.DDriverBillDetails;
import cn.huiyunche.base.service.model.DWaybill;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * @author : suohao
 * Date: 2019/9/19
 */
@Service
public class DriverBillDetailsServiceImpl implements DriverBillDetailsService {
    private static final Logger logger = LoggerFactory.getLogger(DriverBillDetailsServiceImpl.class);
    @Autowired
    private DWaybillService dWaybillService;
    @Autowired
    private DDriverBillDetailsMapper dDriverBillDetailsMapper;
    @Override
    public List<DDriverBillDetails> importExcel(List<DDriverBillDetails> list,String batchId) {
        try {
            List<DDriverBillDetails> listSuccess = new ArrayList<DDriverBillDetails>();
            List<DDriverBillDetails> listErro = new ArrayList<DDriverBillDetails>();

            for (DDriverBillDetails dDriverBillDetails : list) {
                String orderNo = dDriverBillDetails.getOrderNo();
                String orderCode = dDriverBillDetails.getOrderCode();
                if (orderNo==null && orderCode==null){
                    dDriverBillDetails.setErroMsg("指令号和订单号不能同时为空");
                    listErro.add(dDriverBillDetails);
                    continue;
                }
                if (orderNo!=null){
                    if (isNumLegal(orderNo)){
                        orderNo="0"+orderNo;
                        dDriverBillDetails.setOrderNo(orderNo);
                    }
                }
                DWaybill dWaybill = dWaybillService.getByOrderNo(orderNo,orderCode);
                if (dWaybill==null){
                    dDriverBillDetails.setErroMsg("该订单不存在");
                    listErro.add(dDriverBillDetails);
                    continue;
                }
                List<DDriverBillDetails> detailsList = dDriverBillDetailsMapper.getDriverBillDetailsByOrderCode(orderNo,orderCode);
                int count=0;
                for (DDriverBillDetails details : detailsList) {
                    if (dDriverBillDetails.getPayDate().equals(details.getPayDate())){
                        count++;
                    }
                }
                if (count>0){
                    dDriverBillDetails.setErroMsg("该订单对应的结账期已存在");
                    listErro.add(dDriverBillDetails);
                    continue;
                }
                dDriverBillDetails.setUserId(dWaybill.getUserId());
                dDriverBillDetails.setBatchId(batchId);
                listSuccess.add(dDriverBillDetails);
            }
            new Thread(()->{
            for (DDriverBillDetails success : listSuccess) {
                dDriverBillDetailsMapper.insertDriverBillDetails(success);
            }
            }).start();
            return listErro;
        }catch (Exception e){
            logger.error("DriverBillDetailsServiceImpl.import e:" + e.getMessage());
            throw new BusinessException(e.getMessage());
        }
    }

    private  boolean isNumLegal(String str) throws PatternSyntaxException {
        Pattern p = Pattern.compile("1\\d{8}");
        Matcher m = p.matcher(str);
        return m.matches();
    }
    @Override
    public List<DDriverBillDetails> getDriverBillDetailsByOrderCode(String orderNo,String orderCode ) {
        return dDriverBillDetailsMapper.getDriverBillDetailsByOrderCode( orderNo, orderCode);
    }

    @Override
    public Map<String, Object> selectListByConditions(PageVo pageVo, String batchId) {
        Map<String,Object> map = new HashMap<>();
        List<DDriverBillDetails> list = dDriverBillDetailsMapper.selectByConditions(pageVo.getStartIndex(),pageVo.getPageSize(),batchId);

        if (pageVo!=null){
            pageVo.setTotalRecord(dDriverBillDetailsMapper.getDriverBillDetailsCount(batchId));
        }
        map.put("list", list);
        if (null != pageVo) {
            map.put("page", pageVo);
        }
        return map;
    }
}
